package com.huan.spring6.aop.xmlaop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

//切面类
@Aspect //切面类
@Component //ioc容器
public class LogAspect {
    //前置通知
    public void beforeMethod(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("Logger->前置通知，方法名称："+name+",参数："+args);
    }
    // 返回
    public void afterReturning(JoinPoint joinPoint, Object result){
        String name = joinPoint.getSignature().getName();
        System.out.println("Logger->返回增强，方法名称："+name+",返回结果："+result);
    }
    // 异常
    public void afterThrowing(JoinPoint joinPoint, Throwable ex){
        String name = joinPoint.getSignature().getName();
        System.out.println("Logger->异常增强，方法名称："+name+",异常信息："+ex);
    }
    // 后置
    public void afterMethod(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        System.out.println("Logger->后置增强，方法名称："+name);
    }
    // 环绕
    public Object aroundMethod(ProceedingJoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        Object result = null;
        System.out.println("Logger->环绕增强，方法名称："+name+", 参数："+args);
        try {
            System.out.println("环绕增强》目标方法之前执行");
            //调用目标方法
            result = joinPoint.proceed();
            System.out.println("环绕增强》目标方法之后执行");

        }catch (Throwable throwable){
            System.out.println("环绕增强》目标方法异常时执行");
        }finally {
            System.out.println("环绕增强》目标方法执行完成后执行");
        }
        return result;
    }
}
